import { createRouter, createWebHashHistory } from 'vue-router'
import privateRoutes from './routes/index'
import System from '~/utils/System'

const addRouterPath = (list: any[]) => {
    for (let i = 0; i < list.length; i++) {
        const item = list[i]
        System.routerList.push({
            path: item.path,
            meta: item.meta
        })
        if (item.children) addRouterPath(item.children)
    }
}

export const initRouter = () => {
    addRouterPath(privateRoutes)

    const routes = [
        ...privateRoutes,
        {
            path: '/:matchOthers(.*)*',
            redirect: '/error'
        }
    ]

    const router = createRouter({
        history: createWebHashHistory(),
        routes
    })

    router.beforeEach(async (to, from, next) => {
        next()
    })

    System.router = router

    return router
}
